//
//  GJSCImageView.m
//  RobotSDK
//
//  Created by wbb on 2022/10/11.
//

#import "GJSCImageView.h"


@interface GJSCImageView ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
@property (nonatomic, strong) UIScrollView * srcollView;
@property (nonatomic, strong) UIImageView * imageView;
@end
@implementation GJSCImageView

- (IBAction)cancelClick:(id)sender {
    [self removeFromSuperview];
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.backgroundColor = [UIColor blackColor];
    _srcollView = [[UIScrollView alloc]init];
    _srcollView.delegate = self;
    
    _srcollView.userInteractionEnabled = YES;
    _srcollView.showsHorizontalScrollIndicator = YES;//是否显示侧边的滚动栏
    _srcollView.showsVerticalScrollIndicator = NO;
    _srcollView.scrollsToTop = NO;
    _srcollView.scrollEnabled = YES;
    _srcollView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, kSCREENH_HEIGHT );

    [self addSubview:_srcollView];
    [self sendSubviewToBack:_srcollView];
    
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, _srcollView.height)];
    _imageView.userInteractionEnabled = YES;
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_srcollView addSubview:_imageView];

    
    [_srcollView setMinimumZoomScale:1];
    [_srcollView setMaximumZoomScale:3.0f];
    [_srcollView setZoomScale:1 animated:NO];
    
}

- (void)setImage:(UIImage *)image {
    _imageView.image = image;
}


#pragma mark - UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _imageView;
}
//当滑动结束时
//- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
//{
//    //把当前的缩放比例设进ZoomScale，以便下次缩放时实在现有的比例的基础上
//    NSLog(@"scale is %f",scale);
//    [_srcollView setZoomScale:scale animated:NO];
//}

- (void)scrollviewdidzoom:(UIScrollView *)scrollView {

    CGRect frame = _imageView.frame;
    CGFloat imageScaleWidth = scrollView.zoomScale * _imageView.width;
    CGFloat imageScaleHeight = scrollView.zoomScale * _imageView.height;
    frame.origin.y = (self.srcollView.frame.size.height - self.imageView.frame.size.height) > 0 ? (self.srcollView.frame.size.height - imageScaleWidth) * 0.5 : 0;
    frame.origin.x = (self.srcollView.frame.size.width - self.imageView.frame.size.width) > 0 ? (self.srcollView.frame.size.width - imageScaleHeight) * 0.5 : 0;
    self.imageView.frame = frame;

    self.srcollView.contentSize = CGSizeMake(self.imageView.frame.size.width + 30, self.imageView.frame.size.height + 30);
}
@end
